package com.lulin.mall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lulin.mall.ware.vo.MergeVO;
import com.lulin.mall.ware.vo.PurchaseDoneVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lulin.mall.ware.entity.PurchaseEntity;
import com.lulin.mall.ware.service.PurchaseService;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.R;


/**
 * 采购信息
 *
 * @author lulin
 * @email 2032327049@qq.com
 * @date 2022-09-21 17:18:25
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询采购单的状态为：新增 或者 已经分配的采购单信息
     * 地址：http://localhost:8090/app/ware/purchase/unreceive/list?t=1667805242251
     */
    @RequestMapping("/unreceive/list")
    public R queryPageUnreceive(@RequestParam Map<String, Object> params) {
        // 查询采购单的状态为：新增 或者 已经分配的采购单信息
        PageUtils page = purchaseService.queryPageUnreceive(params);

        return R.ok().put("page", page);
    }


    /**
     * 采购单的合并
     *
     * @param mergeVO
     * @return
     */
    @RequestMapping("/merge")
    public R merge(@RequestBody MergeVO mergeVO) {
        Integer flag = purchaseService.merge(mergeVO);
        if (flag == -1) {
            return R.error("合并失败···该采购单不能被合并！");
        }
        return R.ok();
    }

    /**
     * 领取采购单
     * [2,3,4]
     *
     * @return
     */
    @PostMapping("/receive")
    public R receive(@RequestBody List<Long> ids) {
        //领取采购单
        purchaseService.received(ids);
        return R.ok();
    }


    /**
     * 完成采购
     * 数据类型：
     * {
     * id: 1,//采购单
     * items：[{items:1,status:4,reason：},{ }] //采购项
     * }
     *
     * @return
     */
    @PostMapping("/done")
    public R done(@RequestBody PurchaseDoneVO vo) {

        //完成采购
        purchaseService.done(vo);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:purchase:info")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:purchase:delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
